package user.forms;

import helpers.LanguageHelper;
import helpers.ValidateHelper;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;

import entities.User;

public class UserForm {
	public static TreeMap<String, Object> checkValid(HttpServletRequest request, User user) {
		TreeMap<String, Object> form = new TreeMap<String, Object>();
		TreeMap<String, String> check = new TreeMap<String, String>();
		
		LanguageHelper translate = (LanguageHelper) request.getAttribute("translate");
		
		String name = (String) request.getParameter("name");
		String gender = (String) request.getParameter("gender");
		String website = (String) request.getParameter("website");
		String aboutme = (String) request.getParameter("aboutme");
		
		if (name == null || name.equals("")) {
			check.put("name", "Full name is not empty");
			user.setName("");
		} else {
			user.setName(name);
		}
		
		if (website == null || website.equals("")) {
			check.put("website", "Website is not empty");
			user.setWebsite("");
		} else {
			user.setWebsite(website);
		}
		
		if (aboutme == null || aboutme.equals("")) {
			check.put("aboutme", "About me is not empty");
			user.setAboutme("");
		} else {
			user.setAboutme(aboutme);
		}
		
		if (gender != null) {
			try {
				user.setGender(Integer.parseInt(gender));
			} catch (Exception e) {
				check.put("gender", "Gender for user not valid");
			}
		}
		
		form.put("checkValid", check);
		form.put("user", user);
		
		return form;
	}
	
	public static TreeMap<String, Object> checkValidAvatar(HttpServletRequest request, User user) {
		TreeMap<String, Object> form = new TreeMap<String, Object>();
		TreeMap<String, String> check = new TreeMap<String, String>();
		
		LanguageHelper translate = (LanguageHelper) request.getAttribute("translate");
		BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
		
		//Check avatar
	    Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(request);
		List<BlobKey> blobKeys = blobs.get("avatar");
	    try {
	    	if (blobKeys != null && check.size() == 0) {
		    	for (BlobKey blobKey : blobKeys) {
			    	BlobInfo info = new BlobInfoFactory().loadBlobInfo(blobKey);
			    	//Check is image
			    	if (!ValidateHelper.checkImage(info.getFilename())) {
			    		blobstoreService.delete(blobKey);
			    		check.put("avatar", "File is not a image.");
			    	} else {
			    		//Delete old
				    	if (user.getAvatar()!= null && !user.getAvatar().isEmpty()) {
				    		BlobKey blobKeyOld = new BlobKey(user.getAvatar());
				    		try {
				    			blobstoreService.delete(blobKeyOld);
							} catch (Exception e) {
							}
				    		
				    	}
				    	user.setAvatar(blobKey.getKeyString());
			    	}
		    	}
		    } else if (blobKeys != null) {
		    	for (BlobKey blobKey : blobKeys) {
		    		blobstoreService.delete(blobKey);
		    	}
			}
		} catch (Exception e) {
			check.put("avatar", "File is not a image.");
		}
	    
		
		form.put("checkValid", check);
		form.put("user", user);

		return form;
	}
}
